package com.example.minigame_flaviotorres;

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class Game extends View implements Runnable {

	Context context;
	private static final int INTERVAL = 700;	
	public boolean running = true;
	public int tempo = 0;

	public Paint paint;
	public Paint paint2;
	public boolean vencerFase = false;
	public boolean timeIsOver = false;
	
	MainActivity main;
	Activity a;
	
	public Bitmap background;
	
	Context contextActivity;
	
	int explodirMina = 0;
	public int soldados = 0;
	
	private float cx;
	private float cy;
	private int iy2;
	private int ix2;
	private int dx;
	private int dy;
	
	public int count = 0;
	public int limitMax = 3;//in seconds(3 seconds)
	public int limitMin = 0;
	public float limit = limitMax*10;
	boolean counting = true;
	public static boolean temp ;
	
	public int x1,x2,y1,y2,altura1,altura2,largura1,largura2;
	
	Drawable mina,soldado;
	
	public int rnd;
	Random random = new Random();
	private int w;
	private int h;
	
	public Game(Context context){
		super(context);
		
		
		mina = context.getResources().getDrawable(R.drawable.mina);
		soldado = context.getResources().getDrawable(R.drawable.soldado);
		
		this.context = context;
		rnd = (random.nextInt(3-1)+1);

		paint = new Paint();
		paint.setColor(Color.GREEN);

		paint2 = new Paint();
		paint2.setColor(Color.BLACK);
		
		a = (Activity)context;
		
		contextActivity = getContext();				
		
		x1 = 30;
		x2 = 110;
		
		y1 = 60;
		y2 = 140;
		
		altura1 = soldado.getIntrinsicHeight();
		largura1 = soldado.getIntrinsicWidth();
		
		altura2 = mina.getIntrinsicHeight();
		largura2 = mina.getIntrinsicWidth();		
		
		Thread minhaThread = new Thread(this);
		minhaThread.setPriority(Thread.MIN_PRIORITY);
		minhaThread.start();
		
	}
	@Override
	protected void onSizeChanged(int width,int height,int oldw,int oldh){
		super.onSizeChanged(width, height, oldw, oldh);
		
		this.w = getWidth();
		this.h = getHeight();
		 cx = w / 2;
		 cy = h / 2;		 		 	
		
	}		
	
	public void run() {
		while (running) {
			try {
				Thread.sleep(INTERVAL);	
				if(temp == true)
					tempo++;
				
				if(tempo > 60)
					tempo = 60;				
			
				int k = 0;
				if(explodirMina == 5)
				{					
					Intent i = new Intent();	
					a.startActivity(i.setClass(contextActivity, VocePerdeu.class));
					tempo = 0;
					temp = false;
					return;
				
				}	
				if(soldados == 3)
				{
					k++;
					Intent i = new Intent();	
					a.startActivity(i.setClass(contextActivity, VoceGanhou.class));	
					Log.i(null,""+k);
					tempo = 0;
					temp = false;
					return;
				
				}
				
				if(tempo >= 40 && soldados < 3 )
				{				
					Intent i = new Intent();	
					a.startActivity(i.setClass(contextActivity, VocePerdeu.class));				
					tempo = 0;
					temp = false;
					return;
					
					
			
				}
				else if(tempo >= 10 && soldados > 10)
				{
					Intent i = new Intent();	
					a.startActivity(i.setClass(contextActivity, VoceGanhou.class));		
					tempo = 0;
					temp = false;
					return;
				}
				
				
								
			} catch (Exception e) {
				Log.e("Jogo", "Sleep da Thread");
			}
			update();
			
		}
	}
	
	public void update() {
		cx += dx;
		cy += dy;

		if (cx + ix2 > w || cx - ix2 < 0) {
			dx *= -1;
			cx += dx;
		}

		if (cy + iy2 > h || cy - iy2 < 0) {
			dy *= -1;
			cy += dy;
		}	
		rnd = (random.nextInt(3-1)+1);		
			
	
			
		postInvalidate();		
	
	}
	
	public void onDraw(Canvas canvas){
		super.onDraw(canvas);
		canvas.drawPaint(paint);	
		
		//canvas.drawBitmap(background, 0,0, paint);
				
		if (soldado != null)
		{
			if(rnd == 1){
			soldado.setBounds(x1,y1,x1+largura1,y1+altura1);
			soldado.draw(canvas);
			}
		}
		
		if (mina != null)
		{
			if(rnd == 2){
			mina.setBounds(x2,y2,x2+largura2,y2+altura2);
			mina.draw(canvas);
			}
		}			
		
		int x = (int) random.nextInt(this.w-largura1);
		int y = (int) random.nextInt(this.h-altura1);
		
		x1 = x+5;
		y1 = y+10;
		
		int t = (int) random.nextInt(this.w-largura2);
		int k = (int) random.nextInt(this.h-altura2);
		
		x2 = t+5;
		y2 = k+10;			
		
		
		rnd = (random.nextInt(3-1)+1);
		
		if(soldados < 0)
			soldados = 0;		
		
		canvas.drawText("Tempo: " + tempo, 0, 10, paint2);
		canvas.drawText("Soldados: " + soldados + " / 10", 150, 10, paint2);
		
	}
	public boolean onTouchEvent(MotionEvent motionEvent){
		int x = (int) motionEvent.getX();
		int y = (int) motionEvent.getY();
		
		if (motionEvent.getAction() == motionEvent.ACTION_DOWN) {	
			
			if (soldado.copyBounds().contains(x, y)){
				
			/*	int r = (int) (Math.random() * (largura1*2-25));
				int g = (int) (Math.random() * (altura1*2-25));				
				
				x1 = r+rnd;
				y1 = g+rnd;*/
				soldados++;
				counting = true;
				//Destroy(soldado);
			}
			
			if(mina.copyBounds().contains(x,y)){
				
				/*int z = (int) (Math.random() * (largura2*2-25));
				int k = (int) (Math.random() * (altura2*2-25));
				
				x2 = z+rnd;
				y2 = k+rnd;*/
				soldados--;
				explodirMina++;
				counting = true;
				//Destroy(mina);
			}
		}
		 // atualiza o canvas
		return super.onTouchEvent(motionEvent);		
	}		
	
}



